library(logr)
log_open("log_A8.log")
log_code()
####################
#REPLICATION FILES: APPENDIX 8
#Article: "When Does Online Public Diplomacy Succeed? Evidence from China's ‘Wolf Warrior’ Diplomats"
#Authors: Daniel Mattingly and James Sundquist
#This Version: June 18, 2022


####################
#Description:
# This script generates plots shown in Appendix 8 of the article,
# which show acceptable balance on observed covariates.

#The script was written with R version 4.1.2
#It was created and tested on Mac OS X (11.6.5).

library(tidyverse)
library(ggpubr)
library(gridExtra)
library(rio)

#Load data
pooled <- import("twitter_diplomacy_data.csv")
# Ensure levels of treatment variable appear in proper order
pooled$Treatment <- factor(pooled$Treatment, levels = c("control", "prochina", "antius"))
pooled$t_ <- pooled$Treatment

# Give treatment values fuller names
pooled$t_ <- dplyr::recode(pooled$t_, prochina="Foreign aid messages", antius="Anti-US messages", control="control")

# Turn covariate factors into indicator variables
pooled <- pooled %>% rename(female = gender, age = age_category, conservative = left_right, college = education, knowledgable = china_knowledge)
pooled$female <- if_else(pooled$female == "Female", 1, 0)
pooled$age <- recode(pooled$age, `18-24` = 0, `25-29` = 1, `30-39` = 2, `40-49` = 3,
                     `50-59` = 4, `60-69` = 4, `70 or older` = 4)
pooled$college <- if_else(pooled$college == "Graduated from college" | pooled$college == "Obtained a professional degree beyond a college degree",
                          1, 0)
pooled$hawk <- if_else(pooled$hawk_dove == "Enemy", 1,
                       if_else(pooled$hawk_dove == "Rival", 1, 0))
pooled$dove <- if_else(pooled$hawk_dove == "Ally", 1,
                       if_else(pooled$hawk_dove == "Partner", 1, 0))
pooled <- pooled %>% mutate(knowledgable = if_else(knowledgable == "Hu Jintao", 1, 0))
myrows <- is.na(pooled$knowledgable)
pooled$knowledgable[myrows] <- 0


# Fit models
m1 <- lm(female ~ t_, data = pooled)
m2 <- lm(age ~ t_, data = pooled)
m3 <- lm(college ~ t_, data = pooled)
m4 <- lm(dove ~ t_, data = pooled)
m5 <- lm(hawk ~ t_, data = pooled)
m6 <- lm(knowledgable ~ t_, data = pooled)


# Function to make coefficient plots
coefplot_maker_2 <- function(model, title){
  beta <- substring(names(m1$coefficients), 3)
  estimate <- model$coefficients
  se <- coef(summary(model))[, 2]
  lower <- estimate - 1.96*se
  upper <- estimate + 1.96*se
  df <- cbind(beta, estimate, se, lower, upper) %>% as.data.frame()
  df <- df %>% slice(-1)
  df$estimate <- df$estimate %>% as.character() %>% as.numeric()
  df$lower <- df$lower %>% as.character() %>% as.numeric()
  df$upper <- df$upper %>% as.character() %>% as.numeric()
  
  fig1 <- ggplot(data = df, aes(x = estimate, y = beta))
  fig1 <- fig1 +  geom_errorbarh(aes(xmin = lower, xmax = upper, height = 0),size=1.0)+ geom_point(data = df, aes(x = estimate, y = beta),size=2.5) 
  fig1 + geom_vline(xintercept = 0, linetype = "dashed",size=1.0) + theme_bw() + xlab("") + ylab("") +
    ggtitle(as.character(title))
}

# Make individual plots
a <- coefplot_maker_2(m1, "Female")
b <- coefplot_maker_2(m2,  "Age")  
c <- coefplot_maker_2(m3, "College")
d <- coefplot_maker_2(m4, "China dove")  
e <- coefplot_maker_2(m5, "China hawk")  
f <- coefplot_maker_2(m6, "China knowledge")  

# Save images
pdf("FigureA12.pdf", width = 8)
ggarrange(a, b+ rremove("y.text"), c+ rremove("y.text"), 
          d, e+ rremove("y.text"), f+ rremove("y.text"),
          widths = c(3.1, 2, 2),
          ncol = 3, nrow = 2)
dev.off()

log_close()
